﻿package com.projects.xenia.documents_classes;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.GoodRefObject;
import com.projects.xenia.ref_classes.GoodsRefItem;
import com.projects.xenia.ref_classes.SalesRefItem;

import android.content.ContentValues;
import android.database.Cursor;

public class Documents_Order {
	
	public DocHeader Head;
	public DocLine Line;	
	public DocLineAdd LineAdd;
		
	public ArrayList <DocLineAdd>  DocLineAddItemCollection = new ArrayList <DocLineAdd> ();
	
	public HashMap<String, SalesRefItem> SalesCollection = new HashMap<String, SalesRefItem>();
	public HashMap<String, GoodsRefItem> OrderAssortmentCollection = new HashMap<String, GoodsRefItem>();
	public HashMap<String, GoodsRefItem> CustomerAssortmentCollection = new HashMap<String, GoodsRefItem>();
	public HashMap<String, DocLine> DocLineCollection = new HashMap<String, DocLine>();
	
	public ArrayList <String> OffersGoodsList = MainGlobalModule.GetOffersAssortmentsList();
	
	public boolean BackupIsExists () {
		
		Cursor _result = MainGlobalModule.DBE.rawQuery("select id from sxdoc_orders_h where id = ?", new String [] {Head.id});
		return (_result.getCount()>0);
		
	}
	
	public void RestoreDocument () {
	
		MainGlobalModule.DBE.beginTransaction();
		
		MainGlobalModule.DBE.delete("xdoc_orders_h", "id = ?", new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_orders_h select * from sxdoc_orders_h where id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_orders_t", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_orders_t select * from sxdoc_orders_t where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_orders_d", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_orders_d select * from sxdoc_orders_d where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_orders_g", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_orders_g select * from sxdoc_orders_g where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_orders_o", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_orders_o select * from sxdoc_orders_o where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_orders_b", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_orders_b select * from sxdoc_orders_b where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.setTransactionSuccessful();
		MainGlobalModule.DBE.endTransaction();
		
	}
	
	public void BackupDocument () {
		
		MainGlobalModule.DBE.beginTransaction();
		
		MainGlobalModule.DBE.delete("sxdoc_orders_h", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_orders_h select * from xdoc_orders_h where id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_orders_t", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_orders_t select * from xdoc_orders_t where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_orders_d", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_orders_d select * from xdoc_orders_d where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_orders_g", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_orders_g select * from xdoc_orders_g where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_orders_o", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_orders_o select * from xdoc_orders_o where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_orders_b", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_orders_b select * from xdoc_orders_b where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.setTransactionSuccessful();
		MainGlobalModule.DBE.endTransaction();
		
	}
	
	public class DocHeader {
		
		public String id;
		public int docnum;
		public Date date_create;
		public Date date_save;
		public Date date_execute;
		public int state;
		public int verstamp;
		public String acctype;
		public String firms_id;
		public String departments_id;
		public String user_id;
		public String employees_id;
		public String customers_id;
		public int planned;
		public double total_ordersum;
		public double total_restsum;
			
	}
	
	public class DocLine {
		
		public String id;
		public String hdoc_id;
		public int lineno;
		public String goods_id;
		public String goods_feature_id;
		public String goods_measures_id;
		public double factor;
		public double ordervalue;
		public double restvalue;
		public double nextvalue;
		public double price;
		public double order_sum;
		public double rest_sum;
		
		public String goods_descr;
		public String goods_measures_descr;
		
	}
	
	public class DocLineAdd {
		
		public String id;
		public String hdoc_id;
		public String tdoc_id;
		public String ordersgoodsdetails_id;
		
		public String ordersgoodsdetails_descr;
		
	}
	
	public class DocOffersLine {
		
		
		
	}
				
	private int GetLastDocNumByCurrentDate () {
		
		int DocNum = 0;
		Cursor ScalarResult = MainGlobalModule.DBE.rawQuery("select max(docnum) from xdoc_orders_h", null);
		
		if (ScalarResult.getCount()>0) {
			ScalarResult.moveToFirst();
			DocNum = ScalarResult.getInt(0);								
		}

		ScalarResult.close();

		DocNum ++;
		
		return (DocNum);
		
	}	
	
	public void OpenDoc (String Id) {
		
		Head = new DocHeader ();
		Line = new DocLine ();
		LineAdd  = new DocLineAdd ();
		
		Cursor DocumentsResult = MainGlobalModule.DBE.rawQuery("select * from xdoc_orders_h where id = ?",new String [] {Id});
		
		DocumentsResult.moveToFirst();
			
		Head.id = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));
		Head.docnum = DocumentsResult.getInt(DocumentsResult.getColumnIndex("docnum"));
		
		Head.date_create = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_create")));
		Head.date_save = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_save"))); 
		Head.date_execute = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_execute")));			
		
		Head.state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("state"));		
		Head.verstamp = DocumentsResult.getInt(DocumentsResult.getColumnIndex("verstamp"));
		Head.acctype = DocumentsResult.getString(DocumentsResult.getColumnIndex("acctype")); 
		Head.firms_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("firms_id"));
		Head.departments_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("departments_id"));		
		Head.user_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("user_id"));
		Head.employees_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("employees_id"));		
		Head.customers_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("customers_id"));		
		Head.planned = DocumentsResult.getInt(DocumentsResult.getColumnIndex("planned"));
		Head.total_ordersum = DocumentsResult.getDouble(DocumentsResult.getColumnIndex("total_ordersum"));
		Head.total_restsum = DocumentsResult.getDouble(DocumentsResult.getColumnIndex("total_restsum"));
		DocumentsResult.close();
		
		DocLineCollection.clear();
		Cursor DocumentsLineResult = MainGlobalModule.DBE.rawQuery("select a.*,g.descr_long,m.descr from xdoc_orders_t a left outer join xref_goods g on a.goods_id = g.id left outer join xref_goodsmeasures gm on a.goods_measures_id = gm.id left outer join xref_measures m on gm.measures_id = m.id where hdoc_id = ?",new String [] {Id});
		if (DocumentsLineResult.getCount()>0) {
			DocumentsLineResult.moveToFirst();			
			do {
				DocLine DocLineItem = new DocLine();
				DocLineItem.id = DocumentsLineResult.getString(0);
				DocLineItem.hdoc_id = DocumentsLineResult.getString(1);
				DocLineItem.lineno = DocumentsLineResult.getInt(2);
				DocLineItem.goods_id = DocumentsLineResult.getString(3);
				DocLineItem.goods_feature_id = DocumentsLineResult.getString(4);
				DocLineItem.goods_measures_id = DocumentsLineResult.getString(5);
				DocLineItem.factor = DocumentsLineResult.getDouble(6);
				DocLineItem.ordervalue = DocumentsLineResult.getDouble(7);
				DocLineItem.restvalue = DocumentsLineResult.getDouble(8);
				DocLineItem.nextvalue = DocumentsLineResult.getDouble(9);
				DocLineItem.price = DocumentsLineResult.getDouble(10);
				DocLineItem.order_sum = DocumentsLineResult.getDouble(11);
				DocLineItem.rest_sum = DocumentsLineResult.getDouble(12);
				
				DocLineItem.goods_descr = DocumentsLineResult.getString(13); 
				DocLineItem.goods_measures_descr = DocumentsLineResult.getString(14);
				
				DocLineCollection.put(DocLineItem.goods_id, DocLineItem);
				
			}
			while (DocumentsLineResult.moveToNext());
			DocumentsLineResult.close();
		}				
		
		DocLineAddItemCollection.clear();
	
		Cursor DocumentsLineAddResult = MainGlobalModule.DBE.rawQuery("select a.id,a.hdoc_id,a.tdoc_id,a.ordersgoodsdetails_id,b.descr from xdoc_Orders_G a inner join xref_OrdersGoodsDetails b  on a.ordersgoodsdetails_id = b.id where a.hdoc_id = ? ",new String [] {Id});
		if (DocumentsLineAddResult.getCount()>0) {
			DocumentsLineAddResult.moveToFirst();			
			do {
				DocLineAdd DocLineAddItem = new DocLineAdd ();
				DocLineAddItem.id = DocumentsLineAddResult.getString(0);
				DocLineAddItem.hdoc_id = DocumentsLineAddResult.getString(1);
				DocLineAddItem.tdoc_id = DocumentsLineAddResult.getString(2);
				DocLineAddItem.ordersgoodsdetails_id = DocumentsLineAddResult.getString(3);
				DocLineAddItem.ordersgoodsdetails_descr = DocumentsLineAddResult.getString(4);				
				DocLineAddItemCollection.add(DocLineAddItem);				
			}
			while (DocumentsLineAddResult.moveToNext());
			DocumentsLineAddResult.close();
		}
	
		
		BackupDocument (); 
		
	}	
	
		
	public void SetDateExecute () {
		
		int MinDaysToDateExecute = Integer.parseInt(MainGlobalModule.ConstantsCollection.get("MinDaysToDateExecute"));
		
		int DaysOfWorkWeek = Integer.parseInt(MainGlobalModule.ConstantsCollection.get("DaysOfWorkWeek"));
		
		Calendar C = Calendar.getInstance();
		
		C.add(Calendar.DAY_OF_MONTH, MinDaysToDateExecute);		
		
		if ((C.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY) & (DaysOfWorkWeek==6)) {
			C.add(Calendar.DAY_OF_MONTH, 1);
		}

		if ((C.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY) & (DaysOfWorkWeek==5)) {
			C.add(Calendar.DAY_OF_MONTH, 2);
		}		
		
		Head.date_execute = C.getTime();
		
	}
	
	public void NewDoc () {
		
		Head = new DocHeader ();
		Line = new DocLine ();
		LineAdd  = new DocLineAdd ();
		
		Head.id = UUID.randomUUID().toString().toUpperCase();
		Head.docnum = GetLastDocNumByCurrentDate ();			
		Head.date_create = Calendar.getInstance().getTime();
		Head.date_save = Calendar.getInstance().getTime();
		
		Head.date_execute = Calendar.getInstance().getTime();
		SetDateExecute ();
		
		Head.state = 0;
		Head.verstamp = 0;
		Head.acctype = MainGlobalModule.ConstantsCollection.get("DefAccType");
		Head.firms_id = MainGlobalModule.ConstantsCollection.get("DefFirm");
		Head.departments_id = MainGlobalModule.ConstantsCollection.get("DefDepartment");		
		Head.user_id = MainGlobalModule.SettingsCollection.get("ManagerId");
		Head.employees_id = MainGlobalModule.SettingsCollection.get("ManagerId");
		Head.customers_id = null;		
		Head.planned = 0;
		Head.total_ordersum = 0;
		Head.total_restsum = 0;
			
		InsertHeaderIntoDB ();
		
	}

	private void InsertHeaderIntoDB () {
		
		
			ContentValues InsertValues = new ContentValues ();
			InsertValues.put("id", Head.id);
			InsertValues.put("docnum", Head.docnum);		
			InsertValues.put("date_create", MainGlobalModule.DateToSqlString(Head.date_create));
			InsertValues.put("date_save", MainGlobalModule.DateToSqlString(Head.date_save));
			InsertValues.put("date_execute", MainGlobalModule.DateToSqlString(Head.date_execute));		
			InsertValues.put("state", Head.state);
			InsertValues.put("verstamp", Head.verstamp);
			InsertValues.put("acctype", Head.acctype);
			InsertValues.put("firms_id", Head.firms_id);
			InsertValues.put("departments_id", Head.departments_id);
			InsertValues.put("user_id", Head.user_id);
			InsertValues.put("employees_id", Head.employees_id);
			InsertValues.put("customers_id", Head.customers_id);
			InsertValues.put("planned", Head.planned);
			InsertValues.put("total_ordersum", Head.total_ordersum);
			InsertValues.put("total_restsum", Head.total_restsum);
			MainGlobalModule.DBE.insert("xdoc_orders_h", null, InsertValues);
		
	}
	
	public void DeleteDoc () {
		
			Head.state = 3;
			UpdateDoc ();				
		
	}	
		
	public void UpdateDoc () {
		
			ContentValues UpdateValues = new ContentValues ();
			UpdateValues.put("id", Head.id);
			UpdateValues.put("docnum", Head.docnum);		
			UpdateValues.put("date_create", MainGlobalModule.DateToSqlString(Head.date_create));
			UpdateValues.put("date_save", MainGlobalModule.DateToSqlString(Calendar.getInstance().getTime()));
			UpdateValues.put("date_execute",  MainGlobalModule.DateToSqlString(Head.date_execute));		
			UpdateValues.put("state", Head.state);
			UpdateValues.put("verstamp", Head.verstamp);
			UpdateValues.put("acctype", Head.acctype);
			UpdateValues.put("firms_id", Head.firms_id);
			UpdateValues.put("departments_id", Head.departments_id);
			UpdateValues.put("user_id", Head.user_id);
			UpdateValues.put("employees_id", Head.employees_id);
			UpdateValues.put("customers_id", Head.customers_id);
			UpdateValues.put("planned", Head.planned);
			UpdateValues.put("total_ordersum", Head.total_ordersum);
			UpdateValues.put("total_restsum", Head.total_restsum);
			
			MainGlobalModule.DBE.update("xdoc_orders_h", UpdateValues, "id=?",new String[] {Head.id} );			
		
		
	}
	
	public void FinishDoc () {
		
		if (Head.customers_id==null){
		
			MainGlobalModule.DBE.beginTransaction();
			
				MainGlobalModule.DBE.delete("xdoc_orders_h", "id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_orders_t", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_orders_d", "hdoc_id=?", new String []{Head.id});			
				MainGlobalModule.DBE.delete("xdoc_orders_g", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_orders_o", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_orders_b", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_gpslog", "id=?", new String []{Head.id});
				
			MainGlobalModule.DBE.setTransactionSuccessful();
			MainGlobalModule.DBE.endTransaction();
			
			
		}
		
	}

	public void InsertUpdateLine () {			
		
		DocLine DocLineItem = DocLineCollection.get(Line.goods_id); 
		if (DocLineItem != null){			
			Line.id = DocLineItem.id; 	
		}
		
		if (((Line.restvalue + Line.ordervalue) == 0) && (DocLineItem != null)) {						
			MainGlobalModule.DBE.delete("xdoc_orders_t", "id = ?", new String []{Line.id});
			DocLineCollection.remove(Line.goods_id);			 
		}
		else {
			ContentValues InsertUpdateValues = new ContentValues ();
			InsertUpdateValues.put("id", Line.id);
			InsertUpdateValues.put("hdoc_id", Line.hdoc_id);
			InsertUpdateValues.put("lineno", Line.lineno);
			InsertUpdateValues.put("goods_id", Line.goods_id);
			InsertUpdateValues.put("goods_feature_id", Line.goods_feature_id);
			InsertUpdateValues.put("goods_measures_id", Line.goods_measures_id);
			InsertUpdateValues.put("factor", Line.factor);
			InsertUpdateValues.put("ordervalue", Line.ordervalue);
			InsertUpdateValues.put("restvalue", Line.restvalue);
			InsertUpdateValues.put("nextvalue", Line.nextvalue);
			InsertUpdateValues.put("price", Line.price);
			InsertUpdateValues.put("order_sum", Line.order_sum);
			InsertUpdateValues.put("rest_sum", Line.rest_sum);
			
			
			if (DocLineItem!=null){
				MainGlobalModule.DBE.update("xdoc_orders_t", InsertUpdateValues, "id=?",new String[] {Line.id} );
			}
			else {
				DocLineItem = new DocLine ();
				MainGlobalModule.DBE.insert("xdoc_orders_t", null, InsertUpdateValues);
			}
			
			
			DocLineItem.id = Line.id;
			DocLineItem.hdoc_id = Line.hdoc_id;
			DocLineItem.lineno = Line.lineno;
			DocLineItem.goods_id = Line.goods_id;
			DocLineItem.goods_feature_id = Line.goods_feature_id;
			DocLineItem.goods_measures_id = Line.goods_measures_id;
			DocLineItem.factor = Line.factor;
			DocLineItem.ordervalue = Line.ordervalue;
			DocLineItem.restvalue = Line.restvalue;
			DocLineItem.nextvalue = Line.nextvalue;
			DocLineItem.price = Line.price;
			DocLineItem.order_sum = Line.order_sum;
			DocLineItem.rest_sum = Line.rest_sum;
			
			DocLineItem.goods_descr = Line.goods_descr;
			DocLineItem.goods_measures_descr = Line.goods_measures_descr;
			
			DocLineCollection.put(DocLineItem.goods_id,DocLineItem);
			
		}	
		
		CalculateHeadSums (Head.id);
		
	}
	
	private void CalculateHeadSums (String id) {

		Head.state = 1;
		
		Cursor qSum = MainGlobalModule.DBE.query("xdoc_orders_t", new String [] {"sum(order_sum)","sum(rest_sum)"}, "hdoc_id=?", new String [] {Head.id}, null, null, null);
		qSum.moveToFirst();   
		
		Head.total_ordersum = qSum.getDouble(0);
		Head.total_restsum = qSum.getDouble(1);
		
		UpdateDoc ();
		
		
	}
	
	public void GetLastSales () {
		
		SalesCollection.clear();
		
		Cursor hdoc; 
		
		if (Boolean.parseBoolean(MainGlobalModule.ConstantsCollection.get("DefLastSalesByOrder"))) {
			hdoc= MainGlobalModule.DBE.rawQuery ("select max (date(date_create)) from xdoc_orders_h where customers_id = ? and total_ordersum > 0 and date(date_create) < date ('now')",new String []{Head.customers_id});	
		}
		else {
			hdoc= MainGlobalModule.DBE.rawQuery ("select max (date(date_create)) from xdoc_orders_h where customers_id = ? and total_restsum > 0 and date(date_create) < date ('now')",new String []{Head.customers_id});
		}
		
		
		if (hdoc.getCount()>0) {
			hdoc.moveToFirst();
			if (hdoc.isNull(0)) {
				return;
			}
			Cursor Sales = MainGlobalModule.DBE.rawQuery("select distinct t.goods_id , sum (t.ordervalue*t.factor) , sum (t.restvalue) from xdoc_orders_t t inner join xdoc_orders_h h on t.hdoc_id = h.id and date (h.date_create) >= date(?) and date (h.date_create) < date ('now') and h.customers_id = ? group by t.goods_id ",new String []{hdoc.getString(0),Head.customers_id});					
			if (Sales.getCount()>0) { 
				Sales.moveToFirst();				
				do {
					
					SalesCollection.put(Sales.getString(0), new SalesRefItem(Sales.getDouble(2),Sales.getDouble(1)));
					} 
				while (Sales.moveToNext());
					}					
			}
		}
	
	
	public void GetCustomerAssortment () {
		
		CustomerAssortmentCollection = new HashMap<String, GoodsRefItem> ();
		
		Cursor GoodsCursor = MainGlobalModule.GetGoodsRefCursorBySalesTOPN(Head.customers_id, 0, MainGlobalModule.GetCustomerPriceCategoy (Head.customers_id) ,Head.firms_id,Head.acctype,Head.departments_id,0);
		GoodsCursor.moveToFirst();
		
		while (!GoodsCursor.isAfterLast()) {	
			
			GoodsRefItem gi = new GoodsRefItem ();
			
			if (gi.isfolder==0) {
				
				gi.id = GoodsCursor.getString(0);
				gi.parentid = GoodsCursor.getString(1);
				gi.article = GoodsCursor.getString(2);
				gi.descr = GoodsCursor.getString(3);
				gi.isfolder = GoodsCursor.getInt(4);
				gi.weight = GoodsCursor.getDouble(5);
				gi.volume = GoodsCursor.getDouble(6);
				gi.minimum_order = GoodsCursor.getDouble(7);
				gi.price = GoodsCursor.getDouble(8);	
				gi.rest  = GoodsCursor.getDouble(9);				
				gi.measure_base_id  = GoodsCursor.getString(10);				
				gi.measure_base_descr = GoodsCursor.getString(11);
				gi.measure_base_factor = GoodsCursor.getDouble(12);
				gi.rule_factor = 0;
				gi.target = 0;				
				gi.position = OrderAssortmentCollection.size();				
			
				CustomerAssortmentCollection.put(gi.id, gi);	
				
			}						
			
			GoodsCursor.moveToNext();
			
		}
		
		GoodsCursor.close();
		
	}
	
	
	public void GetAssortment () {		
	
		OrderAssortmentCollection = new HashMap<String, GoodsRefItem>();		
				
		Cursor SimpleCursor = MainGlobalModule.DBE.rawQuery("select assortmentslist_id from xref_CustomersAssortments where customers_id = ?", new String [] {Head.customers_id});
		
		if (SimpleCursor.getCount()>0) {
			
			SimpleCursor.moveToFirst();
			
			String AssortmentListId = SimpleCursor.getString(0);
			
			SimpleCursor.close();
			
			Cursor GoodsCursor = MainGlobalModule.GetGoodsRefCursorByAssortmentId(AssortmentListId,MainGlobalModule.GetCustomerPriceCategoy (Head.customers_id) , Head.firms_id, Head.acctype,Head.departments_id, 0);
			
			GoodsCursor.moveToFirst();
			
			while (!GoodsCursor.isAfterLast()) {
				GoodsRefItem gi = new GoodsRefItem ();
				
				if (gi.isfolder==0) {
					
					gi.id = GoodsCursor.getString(0);
					gi.parentid = GoodsCursor.getString(1);
					gi.article = GoodsCursor.getString(2);
					gi.descr = GoodsCursor.getString(3);
					gi.isfolder = GoodsCursor.getInt(4);
					gi.weight = GoodsCursor.getDouble(5);
					gi.volume = GoodsCursor.getDouble(6);
					gi.minimum_order = GoodsCursor.getDouble(7);
					gi.price = GoodsCursor.getDouble(8);	
					gi.rest  = GoodsCursor.getDouble(9);				
					gi.measure_base_id  = GoodsCursor.getString(10);				
					gi.measure_base_descr = GoodsCursor.getString(11);
					gi.measure_base_factor = GoodsCursor.getDouble(12);
					gi.rule_factor = GoodsCursor.getDouble(13);
					gi.target = GoodsCursor.getDouble(14);				
					gi.position = OrderAssortmentCollection.size();				
				
					OrderAssortmentCollection.put(gi.id, gi);	
					
				}						
				
				GoodsCursor.moveToNext();
				
			}
			
			GoodsCursor.close();
			
		}
		
	}	

	public void SetLineDocByGoodRefItem (GoodsRefItem gi) {
		
		DocLine DocLineItem = DocLineCollection.get(gi.id); 
		if (DocLineItem != null){			
			Line.id = DocLineItem.id;
			Line.hdoc_id = DocLineItem.hdoc_id;
			Line.lineno = DocLineItem.lineno;
			Line.goods_id = DocLineItem.goods_id;
			Line.goods_feature_id = DocLineItem.goods_feature_id;
			Line.goods_measures_id = DocLineItem.goods_measures_id;
			Line.factor = DocLineItem.factor;
			Line.ordervalue = DocLineItem.ordervalue;
			Line.restvalue = DocLineItem.restvalue;
			Line.nextvalue = DocLineItem.nextvalue;
			Line.price = DocLineItem.price;
			Line.order_sum = DocLineItem.order_sum;
			Line.rest_sum = DocLineItem.rest_sum;	
			
			Line.goods_descr = DocLineItem.goods_descr;
			Line.goods_measures_descr = DocLineItem.goods_measures_descr;
			
		}
		else
		{
			Line.id = UUID.randomUUID().toString().toUpperCase();
			Line.hdoc_id = Head.id;
			Line.lineno = DocLineCollection.size();			
			Line.goods_id = gi.id;
			Line.goods_feature_id = "";
			Line.goods_measures_id = gi.measure_base_id;
			Line.factor = gi.measure_base_factor;
			Line.ordervalue = 0;
			Line.restvalue = 0;
			Line.nextvalue = 0;
			Line.price = gi.price;
			Line.order_sum = 0;
			Line.rest_sum = 0;
			Line.goods_descr = gi.descr;
			Line.goods_measures_descr = gi.measure_base_descr;
		}
		
	}
			
	public DocLineAdd DocLineAddCollectionFind (String id) {
		
		for (DocLineAdd it :DocLineAddItemCollection ) {
			if (it.ordersgoodsdetails_id.equals(id)&it.tdoc_id.equals(Line.id)){
				return (it);
			}
		}
		
		return null;		
	}
	
	public String DocLineAddCollectionLineString (String id) {
		
		StringBuilder sb = new StringBuilder();
		for (DocLineAdd it :DocLineAddItemCollection ) {
			if (it.tdoc_id.equals(id)){
				sb.append(it.ordersgoodsdetails_descr);
				sb.append(";");
			}
		}
		
		if (sb.length()>0) {
			return sb.toString();
		}
		
		return null;
		
	} 
	
	public void insertUpdateLineAddByKey (String id, String ogd_descr, boolean Remove) {
		
		DocLineAdd dogd = DocLineAddCollectionFind(id);
		
		if (dogd==null) {			
			dogd = new DocLineAdd();
			dogd.id = UUID.randomUUID().toString().toUpperCase();
			dogd.hdoc_id = Head.id;
			dogd.tdoc_id = Line.id;
			dogd.ordersgoodsdetails_id = id;
			dogd.ordersgoodsdetails_descr = ogd_descr;			
			DocLineAddItemCollection.add(dogd);
		}
		else
		{
			if (Remove){
				MainGlobalModule.DBE.delete("xdoc_Orders_G", "tdoc_id = ? and ordersgoodsdetails_id = ?", new String []{dogd.tdoc_id,dogd.ordersgoodsdetails_id});
				DocLineAddItemCollection.remove(dogd);
				return;
			}
		}		
		
		ContentValues InsertUpdateValues = new ContentValues ();
		InsertUpdateValues.put("id", dogd.id);
		InsertUpdateValues.put("hdoc_id", dogd.hdoc_id);
		InsertUpdateValues.put("tdoc_id", dogd.tdoc_id);
		InsertUpdateValues.put("ordersgoodsdetails_id", dogd.ordersgoodsdetails_id);		
		
		if (MainGlobalModule.DBE.update("xdoc_Orders_G", InsertUpdateValues, "tdoc_id = ? and ordersgoodsdetails_id = ?",new String[] {dogd.tdoc_id,dogd.id} ) == 0) {
			MainGlobalModule.DBE.insert("xdoc_Orders_G", null, InsertUpdateValues);	
		}		
		
	}		
	
	
}
